package com.example.lt.common;

import java.io.Serializable;


public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int code;


    private T data;

    private String msg = "";

    private ApiResult(IResultCode ResultCode) {
        this.code = ResultCode.getCode();
    }

    private ApiResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    private ApiResult(IResultCode IResultCode, T data) {
        this.code = IResultCode.getCode();
        this.data = data;
    }

    public static ApiResult<String> failed() {
        return new ApiResult<>(ResultCode.FAIL);
    }

    public static ApiResult<String> success() {
        return new ApiResult<>(ResultCode.SUCCESS);
    }

    public static ApiResult<String> successMsg() {
        return new ApiResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<>(ResultCode.SUCCESS, data);
    }

    public static ApiResult<String> error(String msg) {
        return new ApiResult<>(ResultCode.INTERNAL_SERVER_ERROR, msg);
    }

    public static ApiResult<String> error(int code, String msg) {
        return new ApiResult<>(code, msg);
    }

    public static ApiResult<String> error(IResultCode resultCode) {
        return new ApiResult<>(resultCode);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
